package com.ciwong.epaper.modules.epaper.ui.listenspeak;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.dbbean.LswAnswer;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/12/22.
 */
public class RetryQuestionAdapter extends BaseAdapter {

    private Context mContext;
    //一行显示个数
    private  final  int ROW_COUNT=5;

    private ArrayList<LswAnswer> mRetryQuestionList = new ArrayList<LswAnswer>();

    public RetryQuestionAdapter(Context context,ArrayList<LswAnswer> retryQuestions) {
        mContext=context;
        mRetryQuestionList=retryQuestions;
    }

    public void setRetryQuestionList(ArrayList<LswAnswer> retryQuestions) {
        mRetryQuestionList=retryQuestions;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        if (mRetryQuestionList.size() % ROW_COUNT == 0) {
            return mRetryQuestionList.size() / ROW_COUNT;
        } else {
            return mRetryQuestionList.size() / ROW_COUNT + 1;
        }

    }

    @Override
    public Object getItem(int position) {
        return mRetryQuestionList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ItemQuestionNumber itemQuestionNumber;
        if (convertView == null) {
            itemQuestionNumber = new ItemQuestionNumber(mContext);
        }else {
            itemQuestionNumber= (ItemQuestionNumber) convertView;
        }

        if (position * ROW_COUNT < mRetryQuestionList.size()) {
            List<String> numList = new ArrayList<>();
            for (int i=position * ROW_COUNT;(i<mRetryQuestionList.size() && i<((position+1) * ROW_COUNT));i++) {
//                if (null != mRetryQuestionList.get(i).getQuestionNumber()) {
//                    numList.add(String.valueOf(mRetryQuestionList.get(i).getQuestionNumber()));
//                }
            }
            itemQuestionNumber.update(numList);
        }

//        TextView tx_question_number = (TextView) convertView.findViewById(R.id.tx_question_number);
//        if (position < mRetryQuestionList.size()) {
//            tx_question_number.setText(String.valueOf(mRetryQuestionList.get(position).getQuestionNumber()));
//        }else
//        {
//            tx_question_number.setText(String.valueOf(position+1));
//        }

        return itemQuestionNumber;

    }
}
